/* internal/js/employee/form.js */

function resetFields () {
	document.getElementById('id').value = '';
	document.getElementById('firstname').value = '';
	document.getElementById('lastname').value = '';
	document.getElementById('username').value = '';
	document.getElementById('password').value = '';
	document.getElementById('passcode').value = '';
	document.getElementById('position').selectedIndex = '0';
	selectResultRow(0);
}



function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th>First Name</th>' +
	           '<th>Last Name</th>' +
	           '<th>Username</th>' +
	           '<th>Position</th>' +
	           '</tr>';
	return html;
}

function addResultRow (id,firstname,lastname,username,position) {
	var html =
		'<form id="listEmployeeForm' + id + '">' +
		'<input id="firstname" value="' + firstname + '" type="hidden" />' +
		'<input id="lastname" value="' + lastname + '" type="hidden" />' +
		'<input id="username" value="' + username + '" type="hidden" />' +
		'<input id="position" value="' + position + '" type="hidden" />' +
		'</form>' +
		'<tr id="listEmployeeRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\')">' +
		'<td>' + firstname + '</td>' +
		'<td>' + lastname + '</td>' +
		'<td>' + username + '</td>' +
		'<td>' + position + '</td>' +
		'</tr>';
	return html;
}

function endResultsTable (id) {
	var html =
		'<input id="resultsIds" value="' + id.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}

function selectResultRow(id)
{
	var ids = document.getElementById('resultsIds').value;
	if (ids != '') {
		var idarray = ids.split(',');
		var count = idarray.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listEmployeeRow' + idarray[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listEmployeeRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function showResult (id) 
{
	selectResultRow(id);
	
	var frm = document.getElementById('listEmployeeForm'+id);
	document.getElementById('id').value = id;
	document.getElementById('firstname').value = frm.firstname.value;
	document.getElementById('lastname').value = frm.lastname.value;
	document.getElementById('username').value = frm.username.value;
	document.getElementById('password').value = 'j8&#4i^b/1!c';
	document.getElementById('passcode').value = 'j8&#4i^b/1!c';
	
	var posList = document.getElementById('position');
	var position = frm.position.value;
	for (var loop = 0; loop < posList.length; loop++) {
		if (posList.options[loop].value == position) {
			posList.selectedIndex = loop;
			break;
		}
	}
}

function loadPage () {

}

function buildTable (employees) {
	var html = startResultsTable();
	var count = employees.childNodes.length;
	var ids = new Array(count);
	var loop;
	var id;
	for (loop = 0; loop < count; loop++) {
		var employee = employees.childNodes[loop];
		id = employee.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		ids[loop] = id;
		
		if (employee.getElementsByTagName('username')[0].childNodes.length > 0)
			var username = employee.getElementsByTagName('username')[0].childNodes[0].nodeValue;
		else
			username = '';
		var firstname = employee.getElementsByTagName('firstname')[0].childNodes[0].nodeValue;
		var lastname = employee.getElementsByTagName('lastname')[0].childNodes[0].nodeValue;
		var position;
		if (employee.getElementsByTagName('position')[0].childNodes.length > 0)
			position = employee.getElementsByTagName('position')[0].childNodes[0].nodeValue;
		else
			position = '';
		html += addResultRow(id, firstname, lastname, username, position);
	}
	html += endResultsTable(ids);
	return html;
}

function enableUserPass() {
	if (document.getElementById('position')
				.options[document.getElementById('position').selectedIndex].value == 'Manager') {
		document.employeeForm.username.disabled = false;
		document.employeeForm.password.disabled = false;
	}
	else {
		document.employeeForm.username.disabled = true;
		document.employeeForm.password.disabled = true;
	}
}












